package org.nmgyjt.springboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.nmgyjt.springboot.beans.admin.CsSystemUserCreateBean;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.admin.UserInfo;
import org.nmgyjt.springboot.entity.exception.BizException;
import org.nmgyjt.springboot.entity.protectTarget.ProtectTargetInfo;
import org.nmgyjt.springboot.entity.sms.YuFuMSSendEntity;
import org.nmgyjt.springboot.service.ICsSystemUserService;
import org.nmgyjt.springboot.service.ProtectTargetService;
import org.nmgyjt.springboot.service.impl.SendSMSServiceImpl;
import org.nmgyjt.springboot.utils.IDAASUserUtils;
import org.nmgyjt.springboot.utils.ZWWXUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cs/protectTarget")
@Slf4j
public class ProtectTargetController {

    @Autowired
    private ProtectTargetService protectTargetService;
    @Autowired
    SendSMSServiceImpl smsService;
    @Autowired
    ZWWXUtils zwwxUtils;
    @Autowired
    ICsSystemUserService iCsSystemUserService;

    @GetMapping("/getById")
    public ApiRes getById(Long id) {
        return ApiRes.ok(protectTargetService.getById(id));
    }

    @GetMapping("/getByDepId")
    public ApiRes getByDepId(String depId) {
        return ApiRes.ok(protectTargetService.getByDepId(depId));
    }

    @PostMapping("/saveOrUpdate")
    public ApiRes saveOrUpdate(@RequestBody ProtectTargetInfo protectTargetInfo) {
        boolean first = false;
        boolean dep = false;
        boolean org = false;
        if (!ObjectUtils.isEmpty(protectTargetInfo.getId())) {
            ProtectTargetInfo protectTargetInfo1 = protectTargetService.getById(protectTargetInfo.getId());
            if (ObjectUtils.isEmpty(protectTargetInfo1.getFirstHeadPhone())) {
                first = true;
            } else {
                if (!protectTargetInfo1.getFirstHeadPhone().equals(protectTargetInfo.getFirstHeadPhone())) {
                    first = true;
                }
            }
            if (ObjectUtils.isEmpty(protectTargetInfo1.getDepHeadPhone())) {
                dep = true;
            } else {
                if (!protectTargetInfo1.getDepHeadPhone().equals(protectTargetInfo.getDepHeadPhone())) {
                    dep = true;
                }
            }
            if (ObjectUtils.isEmpty(protectTargetInfo1.getOrgHeadPhone())) {
                org = true;
            } else {
                if (!protectTargetInfo1.getOrgHeadPhone().equals(protectTargetInfo.getOrgHeadPhone())) {
                    org = true;
                }
            }
        }
        protectTargetInfo.setState("0");
        boolean f = protectTargetService.saveOrUpdate(protectTargetInfo);
        if (f) {
            YuFuMSSendEntity sendEntity = new YuFuMSSendEntity();
            List<String> params = new ArrayList<>();
            List<String> phone = new ArrayList<>();
            String ssoAccessToken = zwwxUtils.getGovWxToken();
            if (first) {
                CsSystemUserCreateBean bean = new CsSystemUserCreateBean();
                bean.setCreatedBy(protectTargetInfo.getCreatedBy());
                bean.setOrgID(protectTargetInfo.getDepId());
                bean.setMenuID("1000000012");
                bean.setJob(protectTargetInfo.getFirstJob());
                bean.setName(protectTargetInfo.getFirstHead());
                bean.setOrgName(protectTargetInfo.getProtectName());
                bean.setPhone(protectTargetInfo.getFirstHeadPhone());
                try {
                    UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
                    if (null == info) {
                        info = iCsSystemUserService.createCsSystemUser(bean);
                        log.info("------------------创建用户信息：{}", info);
                    }
                    protectTargetInfo.setFirstHeadId(info.getId());
                    Integer code1 = zwwxUtils.getZWUserInfo(protectTargetInfo.getFirstHeadPhone(), "protect-unit-detail/index?depId=" + protectTargetInfo.getDepId(), ssoAccessToken);
                    protectTargetInfo.setFirstSnycState(code1.toString());
                    phone.add("+86" + protectTargetInfo.getFirstHeadPhone());
                    params.add(protectTargetInfo.getFirstHead());
                    params.add("防护目标" + protectTargetInfo.getProtectName());
                    sendEntity.setTemplateParamSet(params);
                    sendEntity.setPhoneNumSet(phone);
                    sendEntity.setType("notification");
                    smsService.sendMS(sendEntity);
                } catch (Exception e) {
                    throw new BizException("创建统一认证用户失败,请联系系统管理员!");
                }
//
            }
            if (dep) {
                CsSystemUserCreateBean bean = new CsSystemUserCreateBean();
                bean.setCreatedBy(protectTargetInfo.getCreatedBy());
                bean.setOrgID(protectTargetInfo.getDepId());
                bean.setMenuID("1000000012");
                bean.setJob(protectTargetInfo.getDepJob());
                bean.setName(protectTargetInfo.getDepHead());
                bean.setOrgName(protectTargetInfo.getProtectName());
                bean.setPhone(protectTargetInfo.getDepHeadPhone());
                try {
                    UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
                    if (null == info) {
                        info = iCsSystemUserService.createCsSystemUser(bean);
                        log.info("------------------创建用户信息：{}", info);
                    }
                    protectTargetInfo.setDepHeadId(info.getId());
                    Integer code2 = zwwxUtils.getZWUserInfo(protectTargetInfo.getDepHeadPhone(), "protect-unit-detail/index?depId=" + protectTargetInfo.getDepId(), ssoAccessToken);
                    protectTargetInfo.setDepSnycState(code2.toString());
                    phone.clear();
                    params.clear();
                    phone.add("+86" + protectTargetInfo.getDepHeadPhone());
                    params.add(protectTargetInfo.getDepHead());
                    params.add("防护目标" + protectTargetInfo.getProtectName());
                    sendEntity.setTemplateParamSet(params);
                    sendEntity.setPhoneNumSet(phone);
                    sendEntity.setType("notification");
                    smsService.sendMS(sendEntity);
                } catch (Exception e) {
                    throw new BizException("创建统一认证用户失败,请联系系统管理员!");
                }

            }
            if (org) {
                CsSystemUserCreateBean bean = new CsSystemUserCreateBean();
                bean.setCreatedBy(protectTargetInfo.getCreatedBy());
                bean.setOrgID(protectTargetInfo.getDepId());
                bean.setMenuID("1000000012");
                bean.setOrgName(protectTargetInfo.getProtectName());
                bean.setJob(protectTargetInfo.getOrgJob());
                bean.setName(protectTargetInfo.getOrgHead());
                bean.setPhone(protectTargetInfo.getOrgHeadPhone());
                try {
                    UserInfo info = IDAASUserUtils.selectUserInfoFromIDAASByOpenIdV2(bean.getPhone());
                    if (null == info) {
                        info = iCsSystemUserService.createCsSystemUser(bean);
                        log.info("------------------创建用户信息：{}", info);
                    }
                    protectTargetInfo.setOrgHeadId(info.getId());
                    Integer code3 = zwwxUtils.getZWUserInfo(protectTargetInfo.getOrgHeadPhone(), "protect-unit-detail/index?depId=" + protectTargetInfo.getDepId(), ssoAccessToken);
                    protectTargetInfo.setOrgSnycState(code3.toString());
                    phone.clear();
                    params.clear();
                    phone.add("+86" + protectTargetInfo.getOrgHeadPhone());
                    params.add(protectTargetInfo.getOrgHead());
                    params.add("防护目标" + protectTargetInfo.getProtectName());
                    sendEntity.setTemplateParamSet(params);
                    sendEntity.setPhoneNumSet(phone);
                    sendEntity.setType("notification");
                    smsService.sendMS(sendEntity);
                } catch (Exception e) {
                    throw new BizException("创建统一认证用户失败,请联系系统管理员!");
                }
            }
            protectTargetService.saveOrUpdate(protectTargetInfo);
            return ApiRes.ok(protectTargetInfo.getId());
        } else {
            return ApiRes.customFail("保存失败！");
        }
    }

    @GetMapping("/list")
    public ApiRes list() {
        return ApiRes.ok(protectTargetService.list());
    }

    @GetMapping("/deleteByDepId")
    public ApiRes deleteByDepId(String depId) {
        Boolean b = protectTargetService.deleteByDepId(depId);
        if (b) {
            return ApiRes.ok();
        } else {
            return ApiRes.customFail("删除失败");
        }

    }
}
